package sv.com.seguros.dso.asg;

import java.util.HashMap;
import java.util.Map;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import sv.com.seguros.dso.BaseServiceImpl;
import sv.com.seguros.exception.WebException;

public class AsgClienteServiceImpl extends BaseServiceImpl implements AsgClienteService {
	
	@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false, rollbackFor = {Exception.class, WebException.class})
	public void deleteAsgCliente(Long asgClienteId) throws Exception{
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("asgClienteId", asgClienteId);
		String hqlQuery = "delete from AsgAbono where asgPoliza.asgPolizaId in (select asgPolizaId from AsgPoliza where asgCliente.asgClienteId = :asgClienteId)";
		executeNonHibernateQuery(hqlQuery, parameters);
		hqlQuery = "delete from AsgPago where asgPagoId not in (select asgPago.asgPagoId from AsgAbono)";
		executeNonHibernateQuery(hqlQuery, new HashMap<String, Object>());
		hqlQuery = "delete from AsgSeguimiento where asgReclamo.asgReclamoId in " +
				   "	(select asgReclamoId from AsgReclamo where asgPoliza.asgCliente.asgClienteId = :asgClienteId)";
		executeNonHibernateQuery(hqlQuery, parameters);
		hqlQuery = "delete from AsgSeguimiento where asgPendiente.asgPendienteId in " +
		   "	(select asgPendienteId from AsgPendiente where asgPoliza.asgCliente.asgClienteId = :asgClienteId)";
		executeNonHibernateQuery(hqlQuery, parameters);
		hqlQuery = "delete from AsgContacto where asgReclamo.asgReclamoId in " +
		   "	(select asgReclamoId from AsgReclamo where asgPoliza.asgCliente.asgClienteId = :asgClienteId)";
		executeNonHibernateQuery(hqlQuery, parameters);
		hqlQuery = "delete from AsgContacto where asgPendiente.asgPendienteId in " +
		   "	(select asgPendienteId from AsgPendiente where asgPoliza.asgCliente.asgClienteId = :asgClienteId)";
		executeNonHibernateQuery(hqlQuery, parameters);
		hqlQuery = "delete from AsgReclamo where asgPoliza.asgPolizaId in (select asgPolizaId from AsgPoliza where asgCliente.asgClienteId = :asgClienteId)";
		executeNonHibernateQuery(hqlQuery, parameters);
		hqlQuery = "delete from AsgPendiente where asgPoliza.asgPolizaId in (select asgPolizaId from AsgPoliza where asgCliente.asgClienteId = :asgClienteId)";
		executeNonHibernateQuery(hqlQuery, parameters);
		hqlQuery = "delete from AsgPoliza where asgCliente.asgClienteId = :asgClienteId";
		executeNonHibernateQuery(hqlQuery, parameters);
		hqlQuery = "delete from AsgCliente where asgClienteId = :asgClienteId";
		executeNonHibernateQuery(hqlQuery, parameters);
	}

}
